<template>
	<view class="my-task task-page">
		<u-tabs :list="list" :is-scroll="false" :current="current" @change="change"></u-tabs>
		<view v-if="current == 0">
			<view class="search-line m-x-1rem m-t-1rem">
				<u-search v-model="searchText" shape="round" @search="onQuery" @custom="onQuery" />
				<!-- <u-button size="small" class="button-primary" round @click="onQuery">搜索</u-button> -->
			</view>
			<view class="task-list">
				<view v-for="taskItem in taskData" :key="taskItem.id" class="simple-card task-card m-x-1rem"
					@click="onShowDetail(taskItem)">
					<view class="simple-header">
						<view class="simple-title">
							<view class="simple-status">
								<u-tag :text="taskStatusOptions[taskItem.status]" color="#fff" size="mini"
									:border-color="taskStatusColor[taskItem.status]"
									:bg-color="taskStatusColor[taskItem.status]">
								</u-tag>
							</view>
							<view class="simple-title-text">
								<span>{{ taskItem.title }}</span>
							</view>
						</view>
						<!-- <view
			        class="action-detail"
			        @click="onShowDetail(taskItem)"
			      >
			        <van-icon name="arrow" />
			      </view> -->
					</view>
					<view class="simple-card-content" style="display: flex;justify-content: space-between;">
						<view>
							<view class="simple-card-content-row">
								<view>
									<span>类型：</span>
									<span>{{ taskTypeOptions[taskItem.type] }}</span>
								</view>
							</view>
							<view class="simple-card-content-row">
								<view>
									<span>时间：</span>
									<span>{{ parseTime(taskItem['createTime']) }}</span>
								</view>
							</view>
						</view>
						<view style="display: flex;align-items: center;">
							<u-modal v-model="showConfirm" :content="content" @confirm="confirm"
								:show-cancel-button="true"></u-modal>
							<view style="color: #006CFF;" @click.stop="onDelete(taskItem)">
								删除
							</view>
							<view
								style="margin-left: 10px;background-color: #006CFF;border-radius: 15px;color: #fff;padding-top: 5px;padding-bottom: 5px;padding-left: 12px;padding-right: 12px;font-size: 12px;"
								@click="onEdit(taskItem)">
								修改
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<addTask v-if="current == 1"></addTask>
		<!-- <tabbar></tabbar> -->
	</view>
</template>

<script>
	import tabbar from '../../components/tabbar/index.vue';
	import addTask from '../../components/addTask/add.vue'
	import {
		taskStatusColor,
		taskStatusOptions,
		taskTypeOptions
	} from "../../constants/taskConstant";
	export default {
		components: {
			tabbar,
			addTask
		},
		data() {
			return {
				list: [{
					name: '我的任务'
				}, {
					name: '新建任务'
				}],
				current: 0,
				searchText: '',
				taskData: [],
				taskStatusColor: [],
				taskStatusOptions: [],
				taskTypeOptions: [],
				showConfirm: false,
				deltask: {},
				content: ""
			}
		},
		mounted() {
			this.taskStatusColor = taskStatusColor
			this.taskStatusOptions = taskStatusOptions
			this.taskTypeOptions = taskTypeOptions
			this.onQuery()
		},
		methods: {
			change(index) {
				this.current = index;
			},
			onShowDetail(task) {
				uni.navigateTo({
					url: '/pages/task/detail?taskId=' + task.id
				})
			},
			onEdit(task) {
				uni.navigateTo({
					url: '/pages/task/updateTask?taskId=' + task.id
				})
				// this.$router.push({
				// 	name: 'UpdateTask',
				// 	params: {
				// 		taskId: task.id
				// 	}
				// })
			},
			onQuery() {
				this.$request('', '/serviceItems/wechat/task/myself/list', 'get', {
					title: this.searchText
				}, {}, ).then(
					res => {
						const {
							rows
						} = res
						this.taskData = rows
					})
			},
			confirm() {
				this.$request('', '/serviceItems/wechat/task/' + this.deltask.id, 'delete', {}, {}, ).then(res => {
					this.onQuery()
				})
			},
			onDelete(task) {
				this.content = "是否确认删除" + task.title
				this.showConfirm = true
				this.deltask = task
			},
			parseTime(time, pattern) {
				if (arguments.length === 0 || !time) {
					return null
				}
				const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
				let date
				if (typeof time === 'object') {
					date = time
				} else {
					if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
						time = parseInt(time)
					} else if (typeof time === 'string') {
						time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm),
							'')
					}
					if ((typeof time === 'number') && (time.toString().length === 10)) {
						time = time * 1000
					}
					date = new Date(time)
				}
				const formatObj = {
					y: date.getFullYear(),
					m: date.getMonth() + 1,
					d: date.getDate(),
					h: date.getHours(),
					i: date.getMinutes(),
					s: date.getSeconds(),
					a: date.getDay()
				}
				const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
					let value = formatObj[key]
					// Note: getDay() returns 0 on Sunday
					if (key === 'a') {
						return ['日', '一', '二', '三', '四', '五', '六'][value]
					}
					if (result.length > 0 && value < 10) {
						value = '0' + value
					}
					return value || 0
				})
				return time_str
			}
		}
	}
</script>

<style lang="scss">
	@import "~@/styles/variables.scss";

	.my-task {
		overflow: scroll;
		background-color: #fff;
		height: 100vh;
	}
</style>